#include <windows.h>
#include <gdiplus.h> 

using Gdiplus::Graphics;
using Gdiplus::Pen;
using Gdiplus::Color;
using Gdiplus::Point;

#include "drawer.h"

#pragma comment( lib, "gdiplus.lib" )


drawer_t::drawer_t()
{
}

drawer_t::drawer_t(Graphics * g) : graphics_(g), num_cells_(1), h_position_(0), v_position_(0)
	, current_area_(0)
{
	polygon_;
	std::ifstream in("1.map");
	in >> polygon_;

	grid_ = grid_t<>(polygon_, 380);
}

void drawer_t::paint_polygon()
{
	Pen pen(Color(127, 0, 0));
	for(size_t i = 0; i < polygon_.size(); ++i)
	{
		point_t a = scale_point(polygon_[i][0]);
		for(size_t j = 1; j < polygon_[i].size(); ++j)
		{
			point_t b = scale_point(polygon_[i][j]);
			graphics_->DrawLine(&pen, (int)a.x, (int)a.y, (int)b.x, (int)b.y);
			a = b;
		}
		point_t b = scale_point(polygon_[i][0]);
		graphics_->DrawLine(&pen, (int)a.x, (int)a.y, (int)b.x, (int)b.y);
	}
}
	
void drawer_t::clear()
{
	graphics_->Clear(Color(255, 255, 255));
}

point_t drawer_t::scale_point(point_t const & point) const
{
	point_t res = point;
	res.x = (point.x + h_position_) / num_cells_;
	res.y = (point.y + v_position_) / num_cells_;
	return res;
}

void drawer_t::paint()
{
	clear();
	h_position_ = -(grid_[current_area_].center().x - grid_.cell_size() / 2);
	v_position_ = -(grid_[current_area_].center().y - grid_.cell_size() / 2);

	paint_cells();
}

void drawer_t::move(move_kind_t where_to_move)
{
	if(where_to_move == LEFT_M && 
		((current_area_ == 1) || (current_area_ - 1) % grid_.width() != 0))
		--current_area_;
	else if(where_to_move == RIGHT_M && current_area_ < (grid_.size() - 1)
		&& (current_area_ + 1) % grid_.width() != 0)
		++current_area_;
	else if(where_to_move == TOP_M && current_area_ > grid_.width())
		current_area_ -= (grid_.width() + 1);
	else if(where_to_move == BOTTOM_M && current_area_ < (grid_.size() - grid_.width() - 1))
		current_area_ += (grid_.width() + 1);
	paint();
}

void drawer_t::paint_cells()
{
	std::vector<unsigned> idxs;
	idxs.push_back(current_area_);
	for(size_t i = 1; i <= num_cells_; ++i)
	{
		idxs.push_back(idxs[i - 1] + 1);
	}

	for(size_t j = 1; j <= num_cells_; ++j)
	{
		for(size_t i = 0; i <= num_cells_; ++i)
		{
			idxs.push_back(idxs[i] + (grid_.width() + 1) * j);
		}
	}

	Pen pen(Color(127, 0, 0));
	for(size_t j = 0; j < idxs.size(); ++j)
	{
		if(idxs[j] < grid_.size())
		{
			cell_t & cell = grid_[idxs[j]];
			for(size_t i = 0; i < cell.size(); ++i)
			{
				point_t a = scale_point(cell[i].first);
				point_t b = scale_point(cell[i].second);
				graphics_->DrawLine(&pen, (int)a.x, (int)a.y, (int)b.x, (int)b.y);
			}
			point_t corner = scale_point(cell.center());
			corner.x -= grid_.cell_size() / 2;
			corner.y -= grid_.cell_size() / 2;
			graphics_->DrawRectangle(&pen, (float)corner.x, (float)corner.y, grid_.cell_size(), grid_.cell_size());
		}
	}
}

void drawer_t::increase()
{
	if(num_cells_ != 1)
	{
		--num_cells_;
		paint();
	}
}

void drawer_t::decrease()
{
	++num_cells_;
	paint();
}